//
//  HDChangePhoneViewController.swift
//  HuaDaoPeifu
//
//  Created by 刘汉琳 on 16/9/5.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit
import SVProgressHUD

class HDChangePhoneViewController: UIViewController {
    
    var phoneNumberStr = String()
    var inputStrArray = [UILabel]()
    var phoneTextField = UITextField()
    var oldPhoneNumber = String()

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "安全验证"
        view.backgroundColor = UIColor(hex:0xf7f7f7 )
        
        let texLabel = UILabel()
        view.addSubview(texLabel)
        texLabel.snp.makeConstraints { (make) in
            make.top.equalTo(30)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(16)
        }
        texLabel.text = "请验证已绑定手机号"
        texLabel.textAlignment = NSTextAlignment.left
        texLabel.font = UIFont.systemFont(ofSize: 16)
        texLabel.textColor = UIColor(hex:0x999999)
        createPhoneNumberView()
        

    }
    
    func createPhoneNumberView(){
        
        let lengthOfSpace = (AppConstant.screenWidth - 40)/11
        
        
        let topLineView = UIView()
        view.addSubview(topLineView)
        topLineView.snp.makeConstraints { (make) in
            make.top.equalTo(56)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(1)
        }
        topLineView.backgroundColor = UIColor(hex:0xdbdbdb)
        
        let bottomLineView = UIView()
        view.addSubview(bottomLineView)
        bottomLineView.snp.makeConstraints { (make) in
            make.top.equalTo(57 + lengthOfSpace)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(1)
        }
        bottomLineView.backgroundColor = UIColor(hex:0xdbdbdb)
        
        
        view.addSubview(phoneTextField)
        phoneTextField.snp.makeConstraints { (make) in
            make.top.equalTo(57)
            make.left.equalTo(21 + 3 * lengthOfSpace)
            make.width.equalTo(lengthOfSpace * 4)
            make.height.equalTo(lengthOfSpace)

        }
        phoneTextField.becomeFirstResponder()
        phoneTextField.delegate = self
        phoneTextField.autocapitalizationType = .none
        phoneTextField.keyboardType = .numberPad
        phoneTextField.tintColor = UIColor.clear
        phoneTextField.backgroundColor = UIColor.white
        
        for index in 0...11 {
            if index == 11 {
                
                
            }else{
                
                    let spaceLineView = UIView()
                    view.addSubview(spaceLineView)
                    spaceLineView.snp.makeConstraints { (make) in
                        make.top.equalTo(57)
                        make.left.equalTo(20 + CGFloat(index) * lengthOfSpace)
                        make.width.equalTo(1)
                        make.height.equalTo(lengthOfSpace)
                    }
                    spaceLineView.backgroundColor = UIColor(hex:0xdbdbdb)
                    
                    let phoneLabel = UILabel()
                    view.addSubview(phoneLabel)
                    phoneLabel.snp.makeConstraints { (make) in
                        make.top.equalTo(57)
                        make.left.equalTo(21 + CGFloat(index) * lengthOfSpace)
                        make.width.equalTo(lengthOfSpace - 1)
                        make.height.equalTo(lengthOfSpace)
                    }
                    phoneLabel.backgroundColor = UIColor.white
                    phoneLabel.text = (phoneNumberStr as NSString).substring(with: NSMakeRange(index,1))
                    phoneLabel.textAlignment = .center
                    phoneLabel.textColor = UIColor(hex:0x333333)
                if (index >= 3)&&(index <= 6) {
                    
                    inputStrArray.append(phoneLabel)
                    phoneLabel.isHidden = true
                    
                    
                }
            }
            
            let spaceLineView = UIView()
            view.addSubview(spaceLineView)
            spaceLineView.snp.makeConstraints { (make) in
                make.top.equalTo(57)
                make.left.equalTo(20 + CGFloat(index) * lengthOfSpace)
                make.width.equalTo(1)
                make.height.equalTo(lengthOfSpace)
            }
            spaceLineView.backgroundColor = UIColor(hex:0xdbdbdb)

        }
        
        createBtn()
        
    }
    
    func createBtn(){
        
        let nextBtn = HDNextStepButton(title:"下一步")
        view.addSubview(nextBtn)
        nextBtn.snp.makeConstraints { (make) in
            make.top.equalTo(phoneTextField.snp.bottom).offset(50)
            make.left.equalTo(view).offset(40)
            make.right.equalTo(view).offset(-40)
            make.height.equalTo(40)
        }
        nextBtn.addTarget(self, action: #selector(HDChangePhoneViewController.nextBtnClick), for: .touchUpInside)
        
    }
    
    func nextBtnClick(){
        let str = phoneNumberStr.replacingOccurrencesOfString("****", withString: phoneTextField.text!)
        if str == oldPhoneNumber {
            navigationController?.pushViewController(HDRegisterViewController(regType: .resChangePhone), animated: true)
        }else{
            SVProgressHUD.showError("请输入正确的手机号")
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with withEvent: UIEvent?) {
        phoneTextField.resignFirstResponder()
    }

}

extension HDChangePhoneViewController:UITextFieldDelegate{
    
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        var  str = String()
        if string.characters.count == 0 {
            (inputStrArray[range.location] as UILabel).isHidden = true
            return true
        }else{
            str = textField.text! + string
            
        }
        if str.characters.count > 4 {
            return false
        }else{
            let number:Int = str.characters.count
            
            if number > 0 {
                for index in 0..<number {
                    (inputStrArray[index] as UILabel).isHidden = false
                    (inputStrArray[index] as UILabel).text = (str as NSString).substring(with: NSMakeRange(index,1))
                }
            }
        }
        
        return true
    }
    
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        phoneTextField.becomeFirstResponder()
    }
}
